import { _pick } from "mmb";
import { AppCTX } from "../../types/koaExtend";
import {
  openDistributeStore,
  listStoreWithStatByUserIDAsDistributor,
} from "../../service/storeService";
import { listNormalBrandDistributorWithBrandsOfStoreNotOpenByUserID } from "../../service/brandDistributorService";
import pageMW from "../../middleware/pager";
import Router from "koa-router";
const router = new Router();

// 开通分销门店
router.post("/sto/store-open/openDistribute", async (ctx: AppCTX, next) => {
  let { brandId, name, intro, pic, distributorId } = ctx.request.body;
  const { userId } = ctx;

  const store = await openDistributeStore({
    name,
    brandId,
    intro,
    pic,
    distributorId,
    userId,
  });

  ctx.body = {
    ...store,
  };
});

// 获取该账号可开通分销门店的品牌
router.get("/sto/store-open/notOpens", pageMW, async (ctx: AppCTX, next) => {
  const { userId, limit, offset } = ctx;

  const { total, items } =
    await listNormalBrandDistributorWithBrandsOfStoreNotOpenByUserID(userId, {
      limit,
      offset,
    });

  ctx.body = {
    total: total,
    rows: items,
  };
});

// 获取该账号已开通的门店
router.get(
  "/sto/store-open/openedStores",
  pageMW,
  async (ctx: AppCTX, next) => {
    const { userId, limit, offset } = ctx;
    const { brandId } = ctx.params;
    const { total, items } = await listStoreWithStatByUserIDAsDistributor(
      userId,
      {
        limit,
        offset,
      }
    );

    ctx.body = {
      total,
      rows: items,
    };
  }
);

export default router;
